package org.jwcloud.project.module.zx.common.importHelper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

public class ProxyExcelRow {

	private ExcelConfig excelConfig;
	private Sheet sheet;
	private int index = 0;

	public ProxyExcelRow(ExcelConfig excelConfig) {
		super();
		this.excelConfig = excelConfig;
		this.uploadTemplateForm();
	}
	
	public Iterator iterator() {
		return new Itr();
	}
	
	private class Itr implements Iterator{

		private int cursor = -1;
		private int excelWidth = 1;//excel表宽度
		//private int tableWidth = 1;//数据库表宽度--应该放在外层
		
		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			ExcelConfig excelConfig = ProxyExcelRow.this.excelConfig;
			this.excelWidth = excelConfig.getEndColumnIndex();
			if(this.cursor == -1) {
				//init  获取初始值，默认1
				this.cursor = excelConfig.getStartRowIndex() == -1 ? 
						1 : excelConfig.getStartRowIndex();
			}
			int endRowIndex = excelConfig.getEndRowIndex();
			if(cursor <= endRowIndex && endRowIndex != -1) {
				cursor++;
				return true;
			}else {
				Sheet sheet = ProxyExcelRow.this.sheet;
				Row row = sheet.getRow(cursor);
				if(row == null) {
					return false;
				}
				int length = this.excelWidth;
				int nullNum = 0;
				for(int i = 0; i <= length; i++) {
					if(row.getCell(i) == null)
						nullNum++;
				}
				//未全部为空表示当前行有值
				cursor++;
				return nullNum != (length + 1);
			}
//			return false;
		}

		@Override
		public String[] next() {
			// TODO Auto-generated method stub
			Sheet sheet = ProxyExcelRow.this.sheet;
			Row row = sheet.getRow(cursor - 1);
			String[] res = new String[this.excelWidth + 1];
			
			for(int i = 0; i <= this.excelWidth; i++) {
				if(row.getCell(i) != null) {
					row.getCell(i).setCellType(CellType.STRING);
					res[i] = row.getCell(i).getStringCellValue();
				}else {
					res[i] = null;
				}
				
			}
			
			return res;
		}

		
	}
	
	private String uploadTemplateForm()  {
	        boolean isExcel2003 = true;  
//	        System.out.println("file name: " + this.excelConfig.getFile().getOriginalFilename());
	        if (this.excelConfig.getFile().getOriginalFilename().matches("^.+\\.(?i)(xlsx)$")) {
	            isExcel2003 = false;
	        }
	        InputStream is;
			try {
				is = this.excelConfig.getFile().getInputStream();
			
	        Workbook wb = null;
	        if (isExcel2003) {
//	        	System.out.println("excel 2003");
	            wb = new HSSFWorkbook(is);
	        } else {
//	        	System.out.println("excel 2007");
	            wb = new XSSFWorkbook(is);
	        }

			this.sheet = wb.getSheetAt(this.excelConfig.getSheetIndex()); //获取sheet页对象
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}
	
}
